<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			var id_krs;
			id_krs = $('#dg').datagrid('getData');
			id_krs = id_krs.rows[0].id_krs;
			
			$('#dialog').dialog({
				title: 'Form KPRS',
				width: $(window).width() * 0.8,
				height: $(window).height() * 0.99,
				closed: true,
				cache: false,
				href: base_url+'krs/add_kprs/'+id_krs+'/'+$('#s_kd_fakultas').val()+'/'+$('#s_kd_prodi').val()+'/'+$('#s_id_tahunajaran').val()+'/'+$('#nim').combogrid('getValue'),
				modal: true, 
				resizable:true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'krs/save/add';
		}
		// end newData
		
		// editData = function (){
			// var row = $('#dg').datagrid('getSelected');
			// if (row){
				// $('#dialog').dialog({
					// title: 'Edit Data krs ',
					// width: 1280,
					// height: 625,
					// closed: true,
					// cache: false,
					// href: base_url+'krs/edit/'+row.kd_krs,
					// modal: false
				// });
				
				// $('#dialog').dialog('open');  
				// url = base_url+'krs/save/edit';
			// }
		// }
		//end editData
		
		deleteData = function (value){
			if(confirm("Apakah yakin akan menghapusnya?")){
				var response = '';
				$.ajax({ type: "GET",
					 url: base_url+'krs/delete_krs_detail/' + value,
					 async: false,
					 success : function(response){
						var response = eval('('+response+')');
						if (response.success){
							$.messager.show({
								title: 'Success',
								msg: 'Data Berhasil Dihapus'
							});
							$('#dg').datagrid('reload');
						} else {
							$.messager.show({
								title: 'Error',
								msg: response.msg
							});
						}
					 }
				});
			}
		}
		//end deleteData 
		
		
		actionbutton = function(value, row, index){
			var col;
			col = '<a href="#" onclick="deleteData(\''+row.id_krs_detail+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Batal</a>';
			return col;
		}
		
		// load grid
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>krs/grid"});
		});	
		
		// load mahasiswa
		load_mahasiswa = function(){
			$('#nim').combogrid({
				panelWidth:450,
				panelHeight:275,
				value:'',
			 
				idField:'nim',
				textField:'nama',
				url:base_url+'prosedur/getMahasiswa/'+$('#s_kd_prodi').val(),
				columns:[[
					{field:'nim',title:'NIM',width:130},
					{field:'nama',title:'Nama',width:250}
				]],
				onChange: function(value){
					//getsemester();
				}
			});
		}
		
		// load krs
		load_krs = function(){
			$('#dg').datagrid('load',{
				kd_fakultas : $('#s_kd_fakultas').val(),
				kd_prodi : $('#s_kd_prodi').val(),
				id_tahunajaran : $('#s_id_tahunajaran').val(),
				nim : $('#nim').combogrid('getValue')
			});
		}
		
		// load grid
		$(function(){ // init
			$('#dg').datagrid({
				url:"<?=base_url()?>krs/load_krs"
			});
				//$('#dg').datagrid('enableFilter'); 
		});	
		
		// onchange
		$('#s_kd_fakultas').change(function(){
			$('#s_kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#s_kd_fakultas').val());
			//load_matakuliah();
		});
		$('#s_kd_prodi').change(function(){
			//$('#kd_kurikulum').load(base_url+'prosedur/getKurikulumbyProdi/'+$('#s_kd_prodi').val());
			load_mahasiswa();
		});
		$('#s_id_tahunajaran').change(function(){
			
		});
		
		//tombol bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
					<?if($this->mdl_auth->CekAkses(array('menu_id'=>12, 'policy'=>'ADD'))){?>
					{
						iconCls:'icon-add',
						text:'Tambah Matakuliah',
						handler:function(){
							newData();
						}
					}
					<?}?>
				],
				layout:[],
				displayMsg:''
			});
		});
		
		//Kimochi
		<?if($this->session->userdata('kd_fakultas')!='' && $this->session->userdata('kd_prodi')!=''){?>
			load_mahasiswa();
		<?}?>
		
	});	
		
</script>

<table id="dg" title="Form KPRS" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:50,
			pageList:[10,20,30,40,50,100,150,200],
			fit:true,
			toolbar:'#toolbar'
			">
<!--
<table id="dg" class="easyui-treegrid"  data-options="
			rownumbers:true,
			singleSelect:true,
			collapsible:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar',
			view:groupview,
			groupField:'nim',
			">			
-->						
	<thead>
		<tr>
			<th field="id_krs" width="100" hidden="true">id krs</th>
			<th field="id_krs_detail" width="100" hidden="true">id krs detail</th>
			<th field="kd_matakuliah" width="100">Kode Matkul</th>
			<th field="nama_matakuliah" width="300">Nama Matkul</th>
			<th field="sks" width="75">SKS</th>
			<th field="semester" width="75">Semester</th>
			<th field="kd_jenismk" width="75">Jenis Matkul</th>
			<th field="aksi" formatter="actionbutton" align="center" width="75">Aksi</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div>
		<table width="700" border="0">
		  <tr>
			<td>Fakultas</td>
			<td>: 
				<select id="s_kd_fakultas" name="kd_fakultas" style="width:220px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Mahasiswa</td>
			<td>: 
				<input type="textfield" id="nim" name="nim" size="33" value="">
			</td>
		  </tr>
		  <tr>
			<td>Program Studi</td>
			<td>: 
				<select id="s_kd_prodi" name="kd_prodi" style="width:220px;">
					<?=$this->mdl_prosedur->OptionProdi();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td></td>
			<td> 
				
			</td>
		  </tr>
		  <tr>
			<td>Tahun Ajaran</td>
			<td>: 
				<select id="s_id_tahunajaran" name="id_tahunajaran" style="width:220px;">
					<?=$this->mdl_prosedur->OptionTahunAjaran();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td></td>
			<td> 
				<a href="#" onclick="load_krs()" class="easyui-linkbutton" iconCls="icon-search">Load</a>
			</td>
		  </tr>
		</table>
	</div>
</div>
